// pages/address/address.js
import request from '../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address: "",
    goodList: "",
    money:"",
    allCouList:"",
    canUseList:'',
    canIndex:'',
    coupon_money:'',
    realMoney:'',
    showCou:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getAddress()
    this.getAllmoney()
    this.getAllCou()
    this.getRealMoney()
  },
  changeAddress() {
    wx.navigateTo({
      url: '../addresslist/addresslist',
    })
  },
  // 计算总金额
  getAllmoney(){
    let allMoney=0
    this.data.goodList.forEach(item=>{
      allMoney+=Number(item.money)
    })
    // console.log(this.data.goodList);
    this.setData({
      money:allMoney.toFixed(2)
    })
  },
  // 支付
  async payment() {
    this.getAddress()
    if(!this.data.address){
      return wx.showToast({
        title: '请选择收货地址',
        icon:"none"
      })
    }
    let shopping_car_ids=[]
    let shop_cart=wx.getStorageSync('settlement')
    shop_cart.forEach(item=>{
      shopping_car_ids.push(item.shopping_car_id)
    })
    let token=wx.getStorageSync('userInfo').token
    let money=this.data.realMoney>0?this.data.realMoney:0
    let coupon_money=this.data.coupon_money
    let address_id=wx.getStorageSync("address")[0].nationalCode
    let res=await request("/api/generateOrder", {
      token,
      address_id,
      money,
      shopping_car_ids,
      coupon_money
    })
    if(res.code===2000){
      let Payinfo={
        appId:res.data.appid,
        timeStamp:res.data.timeStamp,
        nonceStr:res.data.nonce_str,
        package:`prepay_id=${res.data.prepay_id}`,
        signType:"MD5",
        paySign:res.data.paySign
      }
     wx.requestPayment({
       ...Payinfo,
       success(e){
         if(e.errMsg.includes("ok")){
           wx.showToast({
             title: '支付成功',
           })
           setTimeout(()=>{
            wx.navigateBack()
           },1000)
         }
       }
     })
    }
  },
  // 获取所有优惠券
  async getAllCou(){
    let token=wx.getStorageSync('userInfo').token
    let res=await request("api/userCouponList",{token})
    console.log(res);
    if(res.code===2000){
      this.setData({
        allCouList:res.data
      })
      this.getCanuse()
    }
  },
  // 获取可用优惠券
  getCanuse(){
    let realPrice=Number(this.data.money);
    let canList=[]
    let id=""
    let tem=""
    this.data.allCouList.forEach(item=>{
      if(Number(item.achieve)<realPrice){
        canList.push(item)
      }
    })
    canList.forEach((item,index)=>{
      if(Number(item.reduce)>tem){
        tem=Number(item.reduce)
        id=index
      }
    })
    this.setData({
      canUseList:canList,
      canIndex:id,
      coupon_money:tem
    })
    this.getRealMoney()
  },
  // 选择优惠券
  changeCou(e){
    let changeIndex=e.currentTarget.dataset.index;
    let money=this.data.canUseList[changeIndex].reduce
    console.log(money);
    this.setData({
      canIndex:changeIndex,
      coupon_money:money
    })
    this.getRealMoney()
  },
  // 计算实际金额
  getRealMoney(){
    let primeCost=Number(this.data.money)
    let coupon_money=Number(this.data.coupon_money)
    console.log(primeCost,coupon_money);
    this.setData({
      realMoney:(primeCost-coupon_money).toFixed(2)
    })
  },
  // 选择优惠券
  Cou(){
    this.setData({
      showCou:true
    })
  },
  leav(){
    this.setData({
      showCou:false
    })
  },
  // 选择收货地址
  Selectaddress(){
      wx.navigateTo({
        url: '../addresslist/addresslist',
      })
  },
  // 初始化地址
  getAddress(){
    let addressList = wx.getStorageSync("address")
    let goodlist = wx.getStorageSync("settlement")
    this.setData({
      address: addressList[0],
      goodList: goodlist
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getAddress()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})